import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import { format } from 'date-fns';
import { zhCN } from 'date-fns/locale';

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function formatDate(date: string | Date) {
  return format(new Date(date), 'yyyy年MM月dd日', { locale: zhCN });
}

export function formatMoney(amount: number) {
  return new Intl.NumberFormat('zh-CN', {
    style: 'currency',
    currency: 'CNY',
  }).format(amount);
}

export function calculateProgress(current: number, total: number) {
  if (total <= 0) return 0;
  const progress = (current / total) * 100;
  return Math.min(100, Math.max(0, progress));
}

export function getApiUrl(path: string) {
  // 移除开头的斜杠以避免双斜杠
  const cleanPath = path.startsWith('/') ? path.slice(1) : path;
  return `/ddyx/api/${cleanPath}`;
}
